/*
 * Created on 2005-3-22
 *
 */
package bma.common.langutil.convert.common;

import java.util.LinkedList;
import java.util.List;

import bma.common.langutil.convert.Converter;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class LinkConverter implements Converter {

	private List<Converter> link = new LinkedList<Converter>();

	public LinkConverter() {
		super();
	}

	/**
	 * @return Returns the link.
	 */
	public List<Converter> getLink() {
		return link;
	}

	public void add(Converter b) {
		if (!link.contains(b)) {
			link.add(b);
		}
	}

	@Override
	public boolean canConvert(Class from, Class to) {
		for (Converter c : link) {
			if (c.canConvert(from, to)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public Object convertTo(Object src, Class to) {
		if (src == null)
			return null;

		for (Converter c : link) {
			if (c.canConvert(src.getClass(), to)) {
				return c.convertTo(src, to);
			}
		}
		return null;
	}

}
